/**
 * @author benny.zheng
 * @data 2016-06-06
 * @description 本文件用于方便复制粘贴UI模块之用，请更新这里的说明
 *              另外，考虑到一般是放在js/src/pages/page-name/main.js，因此使用../../
 *              如果不是这个目录，请更改成正确的相对路径
 */
//----------------require--------------

var viewport = require("mlib/dom/viewport"); // viewport
var base = require("mlib/comp/base"); // 基础对象
var parseModule = require("mlib/dom/parseModule"); // 页面模块自动解析
var wechat = require("../plugin/wx/wx-api");

var ajax = require("mlib/io/ajax");
var lazyload = require("mlib/dom/lazyload");
var listRender = require("./ajaxlist.ejs");
var nodataRender = require("./no-data.ejs");


module.exports = function(node, opts) {
    //-----------声明模块全局变量-------------
    var nodeList = null; // 存储所有关键节点
    var that = base();
    var data = null;
	var couponParent = null;
	var couponList = null;
	var loadObj = null;

    //-------------事件响应声明---------------
    var evtFuncs = {

	}

    //-------------子模块实例化---------------
    var initMod = function() {		
          couponParent = document.getElementById("coupon-list");
          couponList = couponParent.querySelector(".coupon-list");
           
		wechat('off').init();
        custFuncs.getData(1,true);
    }

    //-------------绑定事件------------------
    var bindEvents = function() {

	}

    //-------------自定义函数----------------
    var custFuncs = {
			initlazyload:function(){
				loadObj = lazyload(couponParent,{
				   "lazyload" : function(){
							var ocid =couponParent.dataset.ocid,pageindex=couponParent.dataset.page;
							custFuncs.getData(pageindex);
				   }
				});
				loadObj.init();
			},
		    getData:function(pageindex,isFirst){
					if(isFirst){custFuncs.initlazyload();}
					if(that.isLock()) {
						return;
					}
					that.lock();
					ajax({
						url: "/index.php?r=card/list&ocid="+data.ocid+"&pageSize=6&pageIndex="+pageindex,
						data: {}, 
						method: "post",
						onSuccess: function(result) {
							that.unLock();
							if(result.length==0 && isFirst){
								couponList.insertAdjacentHTML('beforeend',nodataRender({}));				
							}else{
								couponList.insertAdjacentHTML('beforeend',listRender({couponlist:custFuncs.formatDate(result),ocid:data.ocid})); 
								couponParent.setAttribute("data-page",parseInt(couponParent.dataset.page)+1);  
								loadObj.work();  
							}							
						},
						onError: function(xmlHttp) {
						   that.unLock();
						}
					});

			},
			formatDate:function(cardata){
				var newArr = [];
				cardata.forEach(function(item){
				 item.endtime = custFuncs.formatTime(parseInt(item.end_time));				 
				 newArr.push(item);				 
				});
				return newArr;
			},
			formatTime:function(time){
				var times = new Date(time*1E3);
				
				return times.getFullYear() + "-" + (times.getMonth()+1)+"-"+times.getDate();			 
			}
    }

    //-------------一切从这开始--------------
    var init = function(_data) {

        data = _data;
        // 根据数据初始化模块
        // opts["render"]({ "title": data["title"] });

        // 找到所有带有node-name的节点
        nodeList = parseModule(node);
        // 子模块实例化
        initMod();
        // 绑定事件
        bindEvents();


    }

    //---------------暴露API----------------
    that.init = init;

    return that;
};